package com.modules.controller.sys;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.controller.MyController;
import com.common.log.LogData;
import com.common.vo.LoginUser;
import com.common.vo.Result;
import com.modules.entity.sys.UserEntity;
import com.modules.service.sys.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 用户
 * @author yyq
 *
 */
@Api(value="/user", tags = "用户管理")
@Controller
@RequestMapping("/sys/user")
public class UserControl extends MyController<UserEntity>{

	@Autowired
	private UserService service;
	
	
	/**
	 * 查询
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/select")
	@ResponseBody
	@ApiOperation(value = "查询接口", notes = "查询接口" , httpMethod = "GET")
	public Result<?> select(UserEntity user){
		if(user.getOid() == null) {
			user.setOid(getUser().getLastOid());
		}
		return service.select(getPage(), user);
	}
	
	/**
	 * 查询单个
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public Result<?> selectById(UserEntity user){
		UserEntity by = service.getById(user.getId());
		return Result.success(by);
	}
	
	/**
	 * 新增
	 * @param request
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "新增")
	@RequestMapping("/add")
	@ResponseBody
	public Result<?> add(UserEntity user){
		return service.insert(user);
	}
	
	/**
	 * 修改
	 * @param request
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "修改")
	@RequestMapping("/edit")
	@ResponseBody
	public Result<?> edit(UserEntity user){
		LoginUser u = getUser();
		if(user.getId() == u.getId()) {
			u.setImg(user.getImg());
			getSession().setAttribute("loginUser", u);
		}
		return service.update(user);
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "单个删除")
	@RequestMapping("/delete")
	@ResponseBody
	public Result<?> delete(UserEntity user){
		service.removeById(user.getId());
		return Result.success("删除成功");
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "批量删除")
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(String ids){
		service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.success("删除成功");
	}
	
	
	

}
